<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Validate;
use app\facade\Redis;

use app\models\{Config as Configs, Banner, Contack, Annou, News, User as Users, Project};

class System extends Base
{
    public function project()
    {
        $model = new Project();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (isset($data['title'])) {
            $list = $list->where('name', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    
    //添加修改
    public function project_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new Project())->allowField(true)->save($data);
            } else {
                $result = (new Project())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Project())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function project_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Project())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function index()
    {
        $model = new Configs();
        $data = $this->request->get();
        $list = $model->paginate(20, false, ['query' => $this->request->param()])->each(function ($item) {
            if ($item['key_value'] == 'bankinfo' || $item['key_value'] == 'adsdividend' || $item['key_value'] == 'plathandfee' || $item['key_value'] == 'lockrateInterval' || $item['key_value'] == 'ethotherhandfee' || $item['key_value'] == 'btcotherhandfee' || $item['key_value'] == 'otherhandfee' || $item['key_value'] == 'jsrhandfee') {
                $item['value'] = json_decode($item['value'], true);
            }
            return $item;
        });
        return view('', ['list' => $list]);
    }

    public function config_edit()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = $this->request->post();
            $model = new Configs();
            $list = $model->where('id', $data['id'])->find();
            if ($list['key_value'] == 'plathandfee') {
                //验证是否存在值
                if (empty($data['value']['coinnum']) || empty($data['value']['coinhandfee'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '参数值为空或不正确，设置失败']);
                }
                $data['value']['coinnum'] =  explode(',', $data['value']['coinnum']);
                $data['value']['coinhandfee'] =  explode(',', $data['value']['coinhandfee']);
                if (count($data['value']['coinnum']) != count($data['value']['coinhandfee'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '参数长度不一致，请重新设置']);
                }
                $data['value'] = json_encode($data['value'], true);
            }
            if ($list['key_value'] == 'ethotherhandfee' || $list['key_value'] == 'btcotherhandfee' || $list['key_value'] == 'otherhandfee' || $list['key_value'] == 'jsrhandfee') {
                //验证是否存在值
                if (empty($data['value']['coinnum']) || empty($data['value']['coinhandfee'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '参数值为空或不正确，设置失败']);
                }
                $data['value'] = json_encode($data['value'], true);
            }
            if ($list['key_value'] == 'lockrateInterval') {
                if (empty($data['value']['money']) || empty($data['value']['rate'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '参数值为空或不正确，设置失败']);
                }
                $data['value']['money'] =  explode(',', $data['value']['money']);
                $data['value']['rate'] =  explode(',', $data['value']['rate']);
                if (count($data['value']['money']) != count($data['value']['rate'])) {
                    return json(['status' => 1, 'data' => [], 'msg' => '参数长度不一致，请重新设置']);
                }
                $data['value'] = json_encode($data['value'], true);
            }
            if ($list['key_value'] == 'adsdividend') {
                //验证是否存在值
                $data['value'] = json_encode($data['value'], true);
            }
            if ($list['key_value'] == 'bankinfo') {
                //验证是否存在值
                $data['value'] = json_encode($data['value'], true);
            }
            if (isset($data['id']) && $data['id'] > 0) {
                $res = $model->allowField(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = $model->allowField(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'data' => [], 'msg' => '更改成功']);
            }
            return json(['status' => 1, 'data' => [], 'msg' => '更改失败']);
        } else {
            $model = new Configs();
            $id = $this->request->get('id/d', 0);
            $list = $model->where('id', $id)->find();
            if (!empty($list)) {
                $list = $list->toArray();
            }
            if ($list['key_value'] == 'bankinfo') {
                $list['value'] = json_decode($list['value'], true);
            }
            if ($list['key_value'] == 'plathandfee') {
                $list['value'] = json_decode($list['value'], true);
                if (!empty($list['value']['coinnum'])) {
                    $list['value']['coinnum'] = implode(',', $list['value']['coinnum']);
                }
                if (!empty($list['value']['coinhandfee'])) {
                    $list['value']['coinhandfee'] = implode(',', $list['value']['coinhandfee']);
                }
            }
            if ($list['key_value'] == 'lockrateInterval') {
                $list['value'] = json_decode($list['value'], true);
                if (!empty($list['value']['money'])) {
                    $list['value']['money'] = implode(',', $list['value']['money']);
                }
                if (!empty($list['value']['rate'])) {
                    $list['value']['rate'] = implode(',', $list['value']['rate']);
                }
            }
            if ($list['key_value'] == 'adsdividend' || $list['key_value'] == 'ethotherhandfee' || $list['key_value'] == 'otherhandfee' || $list['key_value'] == 'btcotherhandfee' || $list['key_value'] == 'jsrhandfee') {
                $list['value'] = json_decode($list['value'], true);
            }
            return view('', ['row' => $list]);
        }
    }

    public function ad()
    {
        $model = new Banner();
        $data = $this->request->get();
        $list = $model->order('id desc')->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function ad_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new Banner())->allowField(true)->save($data);
            } else {
                $result = (new Banner())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Banner())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function ad_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Banner())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }

    public function message()
    {
        $ModelFun = new Contack();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (isset($data['status']) && $data['status']) {
            $list = $list->where('status', $data['status']);
        }
        $list = $list->where('type', 2)->paginate(20, false, ['query' => $this->request->param()])->each(function ($list) {
            $list['mobile'] = (new Users)->where('id', $list['user_id'])->value('mobile');
        });
        return view('', ['list' => $list]);
    }

    public function message_add()
    {
        $rdslist = Redis::get('clickreturn');
        if ($rdslist) {
            return json(['code' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
        }
        Redis::setex('clickreturn', 2, 1);
        $ModelFun = new Contack();
        $data = $this->request->post();
        if (empty($data['id']) || $data['id'] == 0) {
            return json(['status' => 1, 'msg' => 'ID不能为空']);
        }
        if (empty($data['reply'])) {
            return json(['status' => 1, 'msg' => '回复内容不能为空']);
        }
        $list = $ModelFun->where('id', $data['id'])->find();
        if (empty($list)) {
            return json(['status' => 1, 'msg' => '反馈信息不存在']);
        }
        $res = $ModelFun->where('id', $data['id'])->update(['status' => 2, 'reply' => $data['reply']]);
        if ($res) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }

    public function annou()
    {
        $ModelFun = new Annou();
        $data = $this->request->get();
        $list = $ModelFun->order('id desc');
        if (!empty($data['type'])) {
            $list = $list->where('type', $data['type']);
        }
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        $list = $list->paginate(20, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function annou_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new Annou())->allowField(true)->save($data);
            } else {
                $result = (new Annou())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Annou())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function annou_del()
    {
        $id = trim(input('id'), ',');
        $result = (new Annou())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }
}
